package com.side.view;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

import com.example.cgq.business.R;
import com.nineoldandroids.view.ViewHelper;

/**
 * Created by cgq on 2015/10/31.
 */
public class SideMenu extends HorizontalScrollView {

    private boolean mbisOpen;
    private ViewGroup mFilpMenu;
    public ViewGroup mFlipContent;
    private LinearLayout mPage;
    private int mScreenWeigth;
    private int mMenuOffRightLength = 90;//db
    boolean bIsFirstTime = false;
    private int mFlipMenuWeigth;

    /**
     * 为什么需要注释super
     * @param context
     */
    public SideMenu(Context context) {
        //super(context);
        this(context, null);
    }

    /**
     * 使用自定义属性时
     * @param context
     * @param attrs
     * @param defStyleAttr
     */
    public SideMenu(Context context, AttributeSet attrs, int defStyleAttr) {

        super(context, attrs, defStyleAttr);
        mbisOpen = false;
        //get attr for xml
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.SideMenu, defStyleAttr, 0);

        int n = a.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = a.getIndex(i);
            switch (attr) {
                case R.styleable.SideMenu_rightPading:
                    boolean bIsVehice = getIsScreenVehicle();
                    if (bIsVehice) {
                        mMenuOffRightLength = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(
                                TypedValue.COMPLEX_UNIT_DIP, 50, context.getResources().getDisplayMetrics()));/*50 默认值,本次从xml文件读取*/
                    } else {//land
                        mMenuOffRightLength = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 420,
                                context.getResources().getDisplayMetrics());
                    }
                default:
                    break;
            }
        }

        a.recycle();

        //获取屏幕宽度
        Point pScreenSize = new Point(0,0);
        WindowManager wm = (WindowManager) getContext()
                .getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getSize(pScreenSize);
        mScreenWeigth = pScreenSize.x;

        //dp to  px
       /* mMenuOffRightLength = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 90,
                context.getResources().getDisplayMetrics());*/



    }

    public SideMenu(Context context, AttributeSet attrs) {
        //super(context, attrs);
        this(context, attrs, 0);
    }


    public boolean getIsScreenVehicle() {
        if (getResources().getConfiguration().orientation
                == Configuration.ORIENTATION_LANDSCAPE) {
            // If the screen is now in landscape mode
            // 横屏显示中
            return false;
        }
        return true;
    }
    /**
     * 设置子View的宽和高
     * 设置自己的宽和高
     * @param widthMeasureSpec
     * @para m heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        if (!bIsFirstTime)
        {
            mPage = (LinearLayout) getChildAt(0);
            mFilpMenu = (ViewGroup) mPage.getChildAt(0);
            mFlipContent = (ViewGroup) mPage.getChildAt(1);

            //menu  weidth
            mFlipMenuWeigth = mFilpMenu.getLayoutParams().width = mScreenWeigth - mMenuOffRightLength;
            //content  weigth
            mFlipContent.getLayoutParams().width = mScreenWeigth;

            bIsFirstTime = true;
        }

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }


    /**
     * 设置位置 通过设置偏移量 将menu隐藏
     * @param changed
     * @param l
     * @param t
     * @param r
     * @param b
     */
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);

        this.scrollTo(mFlipMenuWeigth, 0);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        int action = ev.getAction();

        switch (action)
        {
            case MotionEvent.ACTION_UP:
                //隐藏在左侧的宽度
                int scrollX = getScrollX();
                if (scrollX > mFlipMenuWeigth / 2) {
                    this.smoothScrollTo(mFlipMenuWeigth, 0);
                    mbisOpen = false;
                }
                else {
                    this.smoothScrollTo(0,0);
                    mbisOpen = true;
                }
//            case MotionEvent.ACTION_SCROLL:
//                Log.i("tag", getScrollX() + "");
//                if (getScrollX() < 260)
                return true;
        }

        return super.onTouchEvent(ev);
    }

    public void toggle() {
        if (mbisOpen) {
            this.smoothScrollTo(mFlipMenuWeigth, 0);
            mbisOpen = false;
        }
        else {
            this.smoothScrollTo(0,0);
            mbisOpen = true;
        }
    }

    /**
     *
     * @param l  移动的距离
     * @param t
     * @param oldl
     * @param oldt
     */
    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
       // super.onScrollChanged(l, t, oldl, oldt);

        boolean bIsVehice = getIsScreenVehicle();

        float scale = 1.0f * l / mFlipMenuWeigth;
        float rightScale = 0.7f + 0.3f * scale;
        float leftScale = 0.1f + (1-scale) ;
        float leftAlpha = 0.6f + 0.4f * (1 - scale);

        // 调用属性动画，设置TranslationX
        ViewHelper.setTranslationX(mFilpMenu, mFlipMenuWeigth * scale * 0.8f);
        if (bIsVehice) {
            ViewHelper.setScaleX(mFilpMenu, leftScale);
            ViewHelper.setScaleY(mFilpMenu, leftScale);
            ViewHelper.setAlpha(mFilpMenu, leftAlpha);
            // 设置content的缩放的中心点
            ViewHelper.setPivotX(mFlipContent, 0);
            ViewHelper.setPivotY(mFlipContent, mFlipContent.getHeight() / 2);
            ViewHelper.setScaleX(mFlipContent, rightScale);
            ViewHelper.setScaleY(mFlipContent, rightScale);
        } else {//land
            ViewHelper.setScaleX(mFilpMenu, leftScale-0.3f);
            ViewHelper.setScaleY(mFilpMenu, leftScale-0.3f);
            ViewHelper.setAlpha(mFilpMenu, leftAlpha);
        }


    }

    public boolean getIsOpen() {
        return mbisOpen;
    }

    public void MoveToleftSide() {
        this.scrollTo(mFlipMenuWeigth,0);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        float y = 0;
        y = ev.getY();
        if (y < 400) {
            return false;
        } else {
            return super.onInterceptTouchEvent(ev);
        }
    }

}
